package com.qiche.controller.common;

import com.qiche.aspect.Result;
import com.qiche.dto.YearWithModels;
import com.qiche.model.CarBrand;
import com.qiche.model.CarLine;
import com.qiche.model.CarModel;
import com.qiche.model.CarYear;
import com.qiche.service.CarBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆品牌控制器
 * @author Linzewen
 * <p>
 * 2018年2月27日
 */
@Api(value = "/carBrands",description = "车辆品牌控制器")
@RestController
@RequestMapping("/carBrands")
public class CarBrandController {
    @Resource
    private CarBrandService carBrandService;

    @ApiOperation(value = "获取所有的品牌信息")
    @GetMapping(value = "/GetAllBrands")
    public Result GetAllBrands() {
        List<CarBrand> datas = carBrandService.selectByExample(null);
        return Result.createBySuccess(datas);
    }

    @ApiOperation(value = "获取所有的热门品牌信息")
    @GetMapping(value = "/GetPopularBrands")
    public Result GetPopularBrands() {
        List<CarBrand> datas = carBrandService.GetPopularBrands();
        return Result.createBySuccess(datas);
    }

    @ApiOperation(value = "根据首字母查找车牌列表")
    @GetMapping(value = "/GetBrandByFirstLetter")
    public Result GetBrandByFirstLette(@RequestParam("firstLetter") String firstLetter) {
        List<CarBrand> datas = carBrandService.GetBrandByFirstLetter(firstLetter);
        return Result.createBySuccess(datas);
    }


    @ApiOperation(value = "根据品牌ID获取所有的车系")
    @GetMapping(value = "/GetLinesByBrandId")
    public Result GetLinesByBrandId(@RequestParam("brandId") Long brandId) {
        List<CarLine> lines = carBrandService.GetLinesByBrandId(brandId);
        return Result.createBySuccess(lines);
    }

    @ApiOperation(value = "根据车系ID获取所有的年份")
    @GetMapping(value = "/GetYearsByLineId")
    public Result GetYearsByLineId(@RequestParam("lineId") Long lineId) {
        List<CarYear> years = carBrandService.GetYearsByLineId(lineId);
        return Result.createBySuccess(years);
    }


    @ApiOperation(value = "根据车系和年份获取所有的品牌")
    @GetMapping(value = "/GetModelByYear")
    public Result GetModelByYear(@RequestParam("lineId") Long lineId, @RequestParam("year") String year) {
        List<CarModel> models = carBrandService.GetModelsByYear(lineId, year);
        return Result.createBySuccess(models);
    }

    @ApiOperation(value = "根据车系ID获取所有的年份,附带各年份下的品牌信息")
    @GetMapping(value = "/GetYearsWithModelByLineId")
    public Result GetModelsWithYearByLineId(@RequestParam("lineId") Long lineId){
        List<CarYear> years = carBrandService.GetYearsByLineId(lineId);
        List<YearWithModels> list=new ArrayList<>();
        for (CarYear year: years
             ) {
            YearWithModels yearWithModels=new YearWithModels();
            yearWithModels.setYear(year.getYearName());
            yearWithModels.setModelList(carBrandService.GetModelsByYear(lineId, year.getYearName()));
            list.add(yearWithModels);
        }
        return Result.createBySuccess(list);
    }

}
